package org.openProcess;

import com.scm.all.export.ProcessAndThreadUtils;
import com.scm.all.export.SystemUtils;
import com.scm.all.export.WindowOperationUtils;
import com.scm.all.struct.TagProcessenTry32;
import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.scene.control.ListView;
import javafx.scene.control.SelectionMode;
import javafx.scene.input.MouseEvent;
import org.window.PaneAndHBoxFrameAndButtonMoudelAddress;
import org.window.PaneControllerAndHBoxStateAndTextValues;

/**
 * 进程列表数据
 */
public class StageWindowsProcessAndListView {
    public static ListView<String> listView = new ListView();
    /**
     * 存放进程句柄
     */
    public static int ProcessIDHandle;
    public static int ProcessID;
    public static ObservableList<String> items = FXCollections.observableArrayList ();
    public static void init(){

        listView.setItems(items);
        listView.setLayoutX(0);
        listView.setLayoutY(0);
        listView.setPrefWidth(282);
        listView.setPrefHeight(460);
    }
    public static void run(){
        items.clear();
        for(TagProcessenTry32 tagProcessenTry32 : ProcessAndThreadUtils.processEnumInfo()){
            items.addAll(tagProcessenTry32.szExeFile);
        }
        listView.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                if(event.getClickCount()==2 && event.getButton().name().equals("PRIMARY")) {

                    PaneControllerAndHBoxStateAndTextValues.text.setText(ProcessAndThreadUtils.pidGetProcessName(ProcessID));
                    ProcessIDHandle = ProcessAndThreadUtils.openProcess(ProcessID);
                    if(ProcessIDHandle>0){
                        System.out.println("进程打开成功");
                        PaneAndHBoxFrameAndButtonMoudelAddress.button.setDisable(false);
                        StageWindowsProcess.stage.hide();
                    }else {
                        SystemUtils.messageBoxExW(WindowOperationUtils.windowGetHwnd(),"进程打开失败","温馨提示",0);
                        items.clear();
                        PaneControllerAndHBoxStateAndTextValues.text.setText("   未检测加载到进程   ");
                        for(TagProcessenTry32 tagProcessenTry32 : ProcessAndThreadUtils.processEnumInfo()){
                            items.addAll(tagProcessenTry32.szExeFile);
                        }
                        PaneAndHBoxFrameAndButtonMoudelAddress.button.setDisable(true);
                        System.out.println("进程打开失败");
                    }


                }
                if(event.getClickCount()==2 && event.getButton().name().equals("SECONDARY")) {
                    PaneAndHBoxFrameAndButtonMoudelAddress.button.setDisable(true);
                    ProcessAndThreadUtils.closeHandle(ProcessIDHandle);
                    items.clear();
                    PaneControllerAndHBoxStateAndTextValues.text.setText("   未检测加载到进程   ");
                    for(TagProcessenTry32 tagProcessenTry32 : ProcessAndThreadUtils.processEnumInfo()){
                        items.addAll(tagProcessenTry32.szExeFile);
                    }
                }
            }
        });
        listView.getSelectionModel().getSelectedItems().addListener(new InvalidationListener() {
            @Override
            public void invalidated(Observable observable) {
                ProcessID = ProcessAndThreadUtils.processNameGetPid(observable.toString().substring(1,observable.toString().length()-1));
//                System.out.println("选择进程：  " + observable.toString().substring(1,observable.toString().length()-1));
            }
        });
    }
}
